
var server = require('../../utils/server');
var seat;
Page({
	data: {
		city:'',
		banner: [],
		goods: [],
		bannerHeight: Math.ceil(290.0 / 750.0 * getApp().screenWidth)
	},
	showCoupon: function (e) {
		wx.navigateTo({
		  url: '../member/coupon/index',
		  success: function(res){
			// success
		  },
		  fail: function() {
			// fail
		  },
		  complete: function() {
			// complete
		  }
		})
	},
	showOrder: function (e) {
		wx.navigateTo({
		  url: '../order/list/list',
		  success: function(res){
			// success
		  },
		  fail: function() {
			// fail
		  },
		  complete: function() {
			// complete
		  }
		})
	},
	showCollect: function (e) {
		wx.navigateTo({
		  url: '../member/collect/collect',
		  success: function(res){
			// success
		  },
		  fail: function() {
			// fail
		  },
		  complete: function() {
			// complete
		  }
		})
	},
	showMine: function (e) {
		wx.switchTab({
			url: "../member/index/index"
		});
	},
	showSeller: function (e) {
		wx.navigateTo({
		  url: '../seller/index',
		  success: function(res){
			// success
		  },
		  fail: function() {
			// fail
		  },
		  complete: function() {
			// complete
		  }
		})
	},
	search: function (e) {
		wx.navigateTo({
			url: "../search/index"
		});
	},
	showCarts: function (e) {
		wx.switchTab({
			url: "../cart/cart"
		});
	},
	onLoad: function (options) {

		//seat = options.seat;
		//wx.showToast({title:seat+"seat"});
		this.loadBanner();
		//this.loadMainGoods();
		this.getInviteCode(options);

		//定位
		this.loadInfo(); 

		var app = getApp();
		app.getOpenId(function () {

			var openId = getApp().globalData.openid;

            server.getJSON("/User/validateOpenid",{openid:openId},function(res){

				if (res.data.code == 200) {
						getApp().globalData.userInfo = res.data.data;
						getApp().globalData.login = true;
						//wx.switchTab({
						//url: '/pages/index/index'
						//});
					}
					else{
						if (res.data.code == '400') {
						// console.log("need register");

						app.register(function () {

                           getApp().globalData.login = true;
						});
					}
					}

			});

		});	
	},
	getInviteCode: function (options) {
		if (options.uid != undefined) {
			wx.showToast({
				title: '来自用户:' + options.uid + '的分享',
				icon: 'success',
				duration: 2000
			})
		}
	},
	loadBanner: function () {

		var that = this;

        server.getJSON("/Index/home",function(res){
var banner = res.data.result.ad;
				var goods = res.data.result.goods;
				var ad = res.data.ad;
				that.setData({
					banner: banner,
					goods: goods,
					ad: ad
				});
		});

		
		
	},
	loadMainGoods: function () {
		var that = this;
		var query = new AV.Query('Goods');
		query.equalTo('isHot', true);
		query.find().then(function (goodsObjects) {
			that.setData({
				goods: goodsObjects
			});
		});
	},
	onShow:function(){
		var that = this;
		var city =wx.getStorageSync('city');

		that.setData({
			city: city
		});

	},
	switchcity:function(){
		 wx.navigateTo({
		  url:"../switchcity/switchcity"
		 });

	},
	clickBanner: function (e) {

		var goodsId = e.currentTarget.dataset.goodsId;
		wx.navigateTo({
			url: "../goods/detail/detail?objectId=" + goodsId
		});
	},
	showDetail: function (e) {
		var goodsId = e.currentTarget.dataset.goodsId;
		wx.navigateTo({
			url: "../goods/detail/detail?objectId=" + goodsId
		});
	},
	showCategories: function () {
		// wx.navigateTo({
		// 	url: "../category/category"
		// });
		wx.switchTab({
			url: "../category/category"
		});
	},
	showGroupList: function () {
		wx.navigateTo({
			url: "../goods/grouplist/list"
		});
	},
	onShareAppMessage: function () {
		return {
			title: '小程序电商系统',
			desc: '一个基于tpshop开发的电商系统',
			path: '/pages/index/index?uid=4719784'
		}
	},
	loadInfo:function(){ 
		// wx.getLocation({
		//   type: 'wgs84',
		//   success: function(res) {
		//   	console.log(res);
		   
		//   }
		// })

	 
		 var page=this
		 wx.getLocation({ 
			 type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标 
			 success: function(res){ 
			 // success 
			 var longitude=res.longitude 
			 var latitude=res.latitude 
			 page.loadCity(longitude,latitude) 
			}, 
		 
		}) 
	}, 
	 loadCity:function(longitude,latitude){ 
		 var page =this
		 wx.request({ 
			 url: 'https://api.map.baidu.com/geocoder/v2/?ak=6XTwxgPPgvEkky3xWaU30zEVt6HTHtFL&location='+latitude+','+longitude+'&output=json', 
			 data: {}, 
			 header:{ 
			 'Content-Type':'application/json'
			 }, 
			 success: function(res){ 
				 // success 
				 // console.log(res); 
				 
				 var city=res.data.result.addressComponent.city; 
				  wx.setStorage({
			          key:"city",
			          data:city
			        });

				 // page.setData({city:city}); 
			 }, 
			 
		 }) 
	 } 
})